<?php
/*
Template Name: Index page Products Scroller
*/
?>

<?php get_header(); ?>

<?php
$aOptions = SofaSuppaStore::initOptions( false );
$prod_description_trim = ( strlen( $aOptions[ 'prod_description_trim' ] ) > 0 ) ? stripslashes( $aOptions[ 'prod_description_trim' ] ) : 0;
$prod_use_description = $aOptions[ 'prod_use_description' ];
$entrycurrency = $aOptions[ 'entrycurrency' ];
// layout
$hp_layout = $aOptions[ 'hp_layout' ]; // 'default', 'wide', 'narrow_scrolled', 'narrow_grided'
$hp_custom_show_title   = $aOptions[ 'hp_custom_show_title' ];
$hp_custom_show_content = $aOptions[ 'hp_custom_show_content' ];
$hp_num_products = ( strlen( $aOptions[ 'hp_num_products' ] ) > 0 ) ? intval( $aOptions[ 'hp_num_products' ] ) : 6;
?>

<!-- main content start -->

	<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
    
    <div id="featured" class="castshadow grid_8">
    
    	<!-- title (if used) start -->
    
		<?php if( $hp_custom_show_title == 'yes' ) { ?>
        <h1><?php the_title(); ?></h1>
        <?php } ?>
        
        <!-- title (if used) end -->
        
        <!-- page content (if used) start -->
        
        <?php if( $hp_custom_show_content == 'yes' ) { ?>
        <?php the_content(); ?>
        <?php } ?>
        
        <!-- page content (if used) end -->
        
        <div class="clear">&nbsp;</div>

		<?php
        // query products
        global $wp_query;
		$args = array(
			'post_type' => 'product',
			'meta_key' => 'featured',
			'meta_value' => 'on',
			'order' => 'DESC',
			'post_status' => 'publish',
			'posts_per_page' => $hp_num_products 
		);
        $temp_query = $wp_query;
        $wp_query = NULL;
        $wp_query = new WP_Query( $args );
        ?>
    
		<?php if ( have_posts() ) : ?>
    
		<?php
        // different classes are used!
        $switch_class = '';
        $count_rows  = 0;
        ?>
        
        <!-- scrolling product thumbnails start -->

        <div class="grid_8 alpha">
        
        	<div id="relatedproducts" class="bordertop grid_8 alpha omega">
            
            	<!-- control button left start -->
        
                <div class="grid_2 alpha">
                	<?php if( $wp_query->post_count > 4 ) { ?>
                    <span class="addtocart prevPage"><a href="#l"><?php _e( "Previous", "sofa_suppastore" ); ?></a></span>
                    <?php } else echo '&nbsp;'; ?>
                </div>
                
                <!-- control button left end -->
                
                <!-- title start -->
                
                <div class="grid_4 center">
                    <h2 class="relatedtitle"><?php _e( "Featured products", "sofa_suppastore" ); ?></h2>
                </div>
                
                <!-- title end -->
                
                <!-- control button right start -->
                
                <div class="grid_2 omega">
                	<?php if( $wp_query->post_count > 4 ) { ?>
                    <span class="addtocart nextPage"><a href="#r"><?php _e( "Next", "sofa_suppastore" ); ?></a></span>
                    <?php } else echo '&nbsp;'; ?>
                </div>
                
                <!-- control button right end -->
                
            </div>
            
            <div class="scrollable">
            
            	<ul class="images items">
                
                	<!-- wordpress sub-loop loop start -->
                
                	<?php
					$count_items = 0;
					while( have_posts() ) : the_post();
					
					// exclude non-featured
					$product_meta_featured = get_post_meta( get_the_ID(), 'featured', true );
					if( $product_meta_featured != 'on' ) continue; // we are about to list only Featured products, ignore all other
					
					// related post details
					$rel_product_meta_labeltype  = get_post_meta( $post->ID, 'labeltype', true );
					$rel_product_meta_labelvalue = get_post_meta( $post->ID, 'labelvalue', true );
					$rel_product_meta_price      = get_post_meta( $post->ID, 'sofa_0_textarea_price', true );
					
					// extract image
					$rel_prod_image_link = '';
					if( has_post_thumbnail( $post->ID ) ) {
						$default_attr = array( 'class' => "size-thumbnail clearfix alignleft", 'alt' => $post->post_title, 'title' => $post->post_title );
						if( $rel_product_meta_labeltype != '' && $rel_product_meta_labelvalue != '' ) {
							$rel_prod_image_link .= ( '<a href="' . get_permalink( $post->ID ) . '" rel="' . $rel_product_meta_labeltype . '|' . $rel_product_meta_labelvalue . '">' );
						} else $rel_prod_image_link .= ( '<a href="' . get_permalink( $post->ID ) . '">' );
						$rel_prod_image_link .= get_the_post_thumbnail( $post->ID, 'thumbnail', $default_attr );
						$rel_prod_image_link .= '</a>';
					}
					?>
                
                	<li>
                        <div class="grid_2 product alpha">
                            <?php echo $rel_prod_image_link; ?>
                            <span class="prodtitle clearfix">
                            	<?php echo $post->post_title; ?>
                            </span>
                        </div>
                    </li> 

					<?php endwhile; ?>
                    
                    <!-- wordpress sub-loop loop end -->

                </ul>
                    
            </div>
        
        </div>
        
        <!-- scrolling product thumbnails end -->
        
        <script type="text/javascript">
        
        jQuery( document ).ready( function() {
            // trigger scrollable function
            jQuery( "div.scrollable" ).scrollable( { size: 4, speed: 300 } );
        } );
        
        </script>
        
    </div>
    
    <?php else: ?>
    
    <!-- no content (featured products) start -->
    
    <div id="featured" class="castshadow grid_8">
    
        <div class="grid_8 alpha">
        	<h1><?php _e( "Nothing found!", "sofa_suppastore" ); ?></h1>
            <p><?php _e( "Sorry but no products found. Take your time, there's gonna be soon to see something listed here.", "sofa_suppastore" ); ?></p>
        </div>
        
    </div>
    
    <!-- no content (featured products) end -->
    
	<?php endif; ?>
    
    <?php
	// backup query aka reset custom query posts loop
	$wp_query = $temp_query;
	$temp_query = NULL;
	?>

	<?php endwhile; endif; ?>
    
<!-- main content end -->

<!-- include sidebar start -->
    
    <?php get_template_part( 'sidebar_index' ); ?>
    
<!-- include sidebar end -->

<?php get_footer(); ?>

